package com.mui.servicer.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mui.dao.MenuDao;
import com.mui.entity.MenuInfo;
import com.mui.servicer.MenuService;

@Service  
@Transactional 
public class MenuServiceImpl implements MenuService{

	@Autowired  
	private MenuDao menuDao; 
	
	@Override
	public MenuInfo read(String name) {
		// TODO Auto-generated method stub
		return menuDao.read(name);
	}
	
	public List<MenuInfo> read(){
		List<MenuInfo> menuList = menuDao.read();
		List<MenuInfo> menuParent = new ArrayList<MenuInfo>();
		List<MenuInfo> menuChlid = new ArrayList<MenuInfo>();
		
		for(MenuInfo menu : menuList){
			if (menu.getParentid() == 0){
				menuParent.add(menu);
			}else{
				menuChlid.add(menu);
			}
		}
		
		for(int i = 0; i < menuParent.size(); i++){
			
			for(int j = 0; j< menuChlid.size(); j++){
				if(menuParent.get(i).getId() == menuChlid.get(j).getParentid()){
					menuParent.get(i).getChildMenu().add(menuChlid.get(j));
				}
				
			}
		}
		
		return menuParent;
	}

}
